package com.doubao.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.doubao.user.entity.UserMembership;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 用户会员信息Mapper接口
 */
@Mapper
public interface UserMembershipMapper extends BaseMapper<UserMembership> {

    /**
     * 更新用户会员状态
     *
     * @param userId 用户ID
     * @param status 状态
     * @return 影响行数
     */
    @Update("UPDATE user_membership SET status = #{status}, updated_at = NOW() WHERE user_id = #{userId} AND status = 1")
    int updateUserMembershipStatus(@Param("userId") Long userId, @Param("status") Integer status);

    /**
     * 扣减聊天次数
     *
     * @param userId 用户ID
     * @param count 扣减数量
     * @return 影响行数
     */
    @Update("UPDATE user_membership SET remaining_chat_count = remaining_chat_count - #{count}, updated_at = NOW() " +
            "WHERE user_id = #{userId} AND status = 1 AND remaining_chat_count >= #{count}")
    int decreaseChatCount(@Param("userId") Long userId, @Param("count") Integer count);

    /**
     * 扣减切换智能体次数
     *
     * @param userId 用户ID
     * @param count 扣减数量
     * @return 影响行数
     */
    @Update("UPDATE user_membership SET remaining_switch_count = remaining_switch_count - #{count}, updated_at = NOW() " +
            "WHERE user_id = #{userId} AND status = 1 AND remaining_switch_count >= #{count}")
    int decreaseSwitchCount(@Param("userId") Long userId, @Param("count") Integer count);
}